package com.yyplatform.admin.platform.schedule.service.impl;

import java.util.List;

import org.modelmapper.ModelMapper;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.stereotype.Service;

import com.yyplatform.admin.platform.schedule.model.dto.JobLogDTO;
import com.yyplatform.admin.platform.schedule.model.dto.JobLogQueryDTO;
import com.yyplatform.admin.platform.schedule.model.entity.JobLogEntity;
import com.yyplatform.admin.platform.schedule.repository.JobLogRepository;
import com.yyplatform.admin.platform.schedule.service.JobLogService;

import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;

/**
 * @author sleepforests@163.com
 * <p>
 * Created on 2024/9/21 22:51
 */
@Service
@Slf4j
@AllArgsConstructor
public class JobLogServiceImpl implements JobLogService {

    private JobLogRepository jobLogRepository;
    private ModelMapper modelMapper;

    @Override
    public JobLogDTO get(Long id) {
        JobLogEntity jobLogEntity = jobLogRepository.getReferenceById(id);
        return modelMapper.map(jobLogEntity, JobLogDTO.class);
    }

    @Override
    public Page<JobLogDTO> query(JobLogQueryDTO pageQueryDTO) {
        Page<JobLogEntity> jobLogEntityPage =
                jobLogRepository.findByJobId(pageQueryDTO.getJobId(), pageQueryDTO.getPageRequest());

        List<JobLogDTO> jobLogDTOList = jobLogEntityPage.stream()
                .map(jobLogEntity -> modelMapper.map(jobLogEntity, JobLogDTO.class))
                .toList();
        return new PageImpl<>(jobLogDTOList, jobLogEntityPage.getPageable(), jobLogEntityPage.getTotalElements());
    }

    @Override
    public void save(JobLogEntity jobLogEntity) {
        jobLogRepository.save(jobLogEntity);
    }

    @Override
    public void delete(Long id) {
        jobLogRepository.deleteById(id);
    }

    @Override
    public void deleteByJobId(Long jobId) {
        jobLogRepository.deleteByJobId(jobId);
    }
}
